package com.codeest.geeknews.presenter.zhihu;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.test.rule.ActivityTestRule;

import com.codeest.geeknews.EspressoDaggerMockRule;
import com.codeest.geeknews.app.App;
import com.codeest.geeknews.di.component.AppComponent;
import com.codeest.geeknews.di.component.DaggerAppComponent;
import com.codeest.geeknews.di.module.AppModule;
import com.codeest.geeknews.model.DataManager;
import com.codeest.geeknews.presenter.main.MainPresenter;
import com.codeest.geeknews.ui.main.activity.MainActivity;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * Created by admin on 2017/7/6.
 */

public class DailyPresenterTest {
    private final static String TAG = "DailyPresenterTest";

    @Rule
    public EspressoDaggerMockRule rule = new EspressoDaggerMockRule();

    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, false, false);


    @Before
    public void init() {
    }

    @Test
    public void testOnCreate() {
        final MainActivity activity = activityRule.launchActivity(null);
        AppModule mockAppModule = mock(AppModule.class);
        MainPresenter mockLoginPresenter = mock(MainPresenter.class);
        //mockLoginPresenter.attachView(activity);
        Mockito.when(mockAppModule.provideMainPresenter(any(DataManager.class))).thenReturn(mockLoginPresenter);  //当mockAppModule的provideMainPresenter()方法被调用时，让它返回mockLoginPresenter
        AppComponent appComponent = DaggerAppComponent.builder().appModule(mockAppModule).build();  //用mockAppModule来创建DaggerAppComponent
        ((App) activity.getApplication()).setComponent(appComponent);
        try {
            PackageManager pm = activity.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), PackageManager.GET_ACTIVITIES);
            String versionName = pi.versionName;
            verify(mockLoginPresenter).checkVersion(versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }


}
